package com.itic.mall.system.product.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.sys.common.service.OutCommonService;
import com.itic.appbase.applications.sys.company.persistence.model.Company;
import com.itic.appbase.applications.sys.company.persistence.model.CompanyExample;
import com.itic.appbase.applications.sys.company.service.CompanyService;
import com.itic.appbase.framework.common.constants.Constants;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.exception.definition.ParameterErrorException;
import com.itic.appbase.framework.utils.DateHelper;
import com.itic.appbase.framework.utils.IdGen;
import com.itic.appbase.framework.utils.NumberStringHelper;
import com.itic.appbase.framework.utils.RandomOrderNumHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.UserSessionHelper;
import com.itic.mall.system.category.persistence.model.Category;
import com.itic.mall.system.category.service.CategoryService;
import com.itic.mall.system.product.persistence.model.Product;
import com.itic.mall.system.product.persistence.model.ProductExample;
import com.itic.mall.system.product.persistence.model.ProductExample.Criteria;
import com.itic.mall.system.product.service.ProductService;
import com.itic.mall.system.productproperty.persistence.model.ProductProperty;
import com.itic.mall.system.productproperty.persistence.model.ProductPropertyExample;
import com.itic.mall.system.productproperty.service.ProductPropertyService;
import com.itic.mall.system.productpropertyvalue.persistence.model.ProductPropertyValue;
import com.itic.mall.system.productpropertyvalue.persistence.model.ProductPropertyValueExample;
import com.itic.mall.system.productpropertyvalue.service.ProductPropertyValueService;
import com.itic.mall.system.productsku.persistence.model.ProductSku;
import com.itic.mall.system.productsku.persistence.model.ProductSkuExample;
import com.itic.mall.system.productsku.service.ProductSkuService;


@Controller
@RequestMapping(value="/system/product")
public class ProductController
     extends BaseController {
	@Autowired
	private CategoryService categoryService;
	
    @Autowired
    private ProductService productService;

    @Autowired
    private ProductPropertyService productPropertyService;
    
    @Autowired
    private ProductSkuService productSkuService;
    
    @Autowired
    private ProductPropertyValueService productPropertyValueService;

    @Autowired
    private CompanyService companyService;
    

    @Autowired
   	protected OutCommonService outCommonService;
       
       //////////////////////////////////////////////////////常量区////////////////////////////////////////////////////////////////
       private static final String MODEL_NAME_CHINA = "商品管理";//中文名
       private static final String MODEL_NAME＿ENGLISH = "product";//英文名
       private static final String PAGE_COMMON_PATH = "/mall/system/product/product";//通用路径
       private static final String PARENT_COMMON_URL = "/system/category/";//通用路径
       private static final String COMMON_URL = "/system/product/";//通用路径
       
       //////////////////////////////////////////////////////页面跳转区////////////////////////////////////////////////////////////
      
       /*
        * 主页面
        */
       @RequestMapping(value="/init")
       public String init(Model model){
       	model.addAttribute("name_china", MODEL_NAME_CHINA);
       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
       	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
       	model.addAttribute("common_url", COMMON_URL);
       	return PAGE_COMMON_PATH+"Main";
       }
       
       /*
        * 主页面
        */
       @RequestMapping(value="/initList")
       public String initList(String categoryId,Model model){
       	model.addAttribute("categoryId", categoryId);
       	model.addAttribute("name_china", MODEL_NAME_CHINA);
       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
       	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
       	model.addAttribute("common_url", COMMON_URL);
       	return PAGE_COMMON_PATH+"List";
       }
       
       /*
        * 添加页面
        */
       @RequestMapping(value="/initAdd")
       public String initAdd(String categoryId,Model model){
       	if( StringHelper.isNotBlank(categoryId) ){
       		model.addAttribute("categoryId", categoryId);
       	}
       	//添加序号
       	ProductExample example = new ProductExample();
       	example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);
       	List<Product> list = productService.list(example);
       	int size = list.size();
       	model.addAttribute("reorder", size+1);
       	model.addAttribute("name_china", MODEL_NAME_CHINA);
       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
       	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
       	model.addAttribute("common_url", COMMON_URL);
       	return PAGE_COMMON_PATH+"Add";
       }
       
       /*
        * 编辑页面
        */
       @RequestMapping(value="/initEdit")
       public String initEdit(String id , String categoryId, Model model){
       	if( StringHelper.isNotBlank(id) ) {
       		Product temp = productService.get(id);
       		
       		ProductSkuExample example = new ProductSkuExample();
       		example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andProductIdEqualTo(temp.getId()).andIsMainEqualTo(DBConst.TRUE);
       		List<ProductSku> list = productSkuService.list(example);
       		model.addAttribute("editData",iticJsonMapper.toJson(temp));
       		ProductSku main = list.get(0);
       		ProductVo pp = new ProductVo();
       		pp.setId(main.getId());
       		pp.setTitle("主款式");
       		pp.setMainSku(main);
       		
       		List<SkusVo> skusVo = new ArrayList<SkusVo>();
       		
       		example.clear();
       		example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andProductIdEqualTo(temp.getId()).andIsMainNotEqualTo(DBConst.TRUE);
       		List<ProductSku> skus = productSkuService.list(example);
       		if( skus.size() > 0 ){
       			
       			for( int i=0;i<skus.size();i++ ){
       				SkusVo tttmp = new SkusVo();
       				tttmp.setId(skus.get(i).getId());
       				int j=i+2;
       				tttmp.setTitle("款式"+j);
       				skusVo.add(tttmp);
       			}
       		}
       		pp.setSkus(skusVo);
       		
       		model.addAttribute("mainSku",iticJsonMapper.toJson(pp));
       		
       		model.addAttribute("name_china", MODEL_NAME_CHINA);
           	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
           	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
           	model.addAttribute("common_url", COMMON_URL);
           	model.addAttribute("categoryId", categoryId);
       	}else {
               throw new ParameterErrorException();
           }
       	return PAGE_COMMON_PATH+"Edit";
       }
       
       /*
        * 编辑页面
        */
       @RequestMapping(value="/initHongbao")
       public String initHongbao(String id , String categoryId, Model model){
       	if( StringHelper.isNotBlank(id) ) {
       		Product temp = productService.get(id);
       		if( DateHelper.isGreater(DateHelper.formatDateTime(new Date()), temp.getStopTime()+" 23:59:59")){
       			model.addAttribute("isExpired","1");
       		}else{
       			model.addAttribute("isExpired","0");
       		}
       		model.addAttribute("editData",iticJsonMapper.toJson(temp));
       		model.addAttribute("name_china", MODEL_NAME_CHINA);
           	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
           	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
           	model.addAttribute("common_url", COMMON_URL);
           	model.addAttribute("categoryId", categoryId);
       	}else {
               throw new ParameterErrorException();
           }
       	return PAGE_COMMON_PATH+"Hongbao";
       }
       
       /*
        * 编辑页面
        */
       @RequestMapping(value="/initOrder")
       public String initOrder(String id , String categoryId, Model model){
       	if( StringHelper.isNotBlank(id) ) {
       		Product temp = productService.get(id);
       		model.addAttribute("viewData",iticJsonMapper.toJson(temp));
       		model.addAttribute("name_china", MODEL_NAME_CHINA);
           	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
           	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
           	model.addAttribute("common_url", COMMON_URL);
           	model.addAttribute("categoryId", categoryId);
       	}else {
               throw new ParameterErrorException();
           }
       	return PAGE_COMMON_PATH+"Order";
       }
       
       
       /*
        * 编辑页面
        */
       @RequestMapping(value="/initBaokuan")
       public String initBaokuan(String id , String categoryId, Model model){
       	if( StringHelper.isNotBlank(id) ) {
       		Product temp = productService.get(id);
       		
       		model.addAttribute("editData",iticJsonMapper.toJson(temp));
       		model.addAttribute("name_china", MODEL_NAME_CHINA);
           	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
           	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
           	model.addAttribute("common_url", COMMON_URL);
           	model.addAttribute("categoryId", categoryId);
       	}else {
               throw new ParameterErrorException();
           }
       	return PAGE_COMMON_PATH+"Baokuan";
       }
       
       
       /*
        * 编辑页面
        */
       @RequestMapping(value="/initJingxuan")
       public String initJingxuan(String id , String categoryId, Model model){
       	if( StringHelper.isNotBlank(id) ) {
       		Product temp = productService.get(id);
       		model.addAttribute("editData",iticJsonMapper.toJson(temp));
       		model.addAttribute("name_china", MODEL_NAME_CHINA);
           	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
           	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
           	model.addAttribute("common_url", COMMON_URL);
           	model.addAttribute("categoryId", categoryId);
       	}else {
               throw new ParameterErrorException();
           }
       	return PAGE_COMMON_PATH+"Jingxuan";
       }
       
       /*
        * 查看页面
        */
       @RequestMapping(value="/initView")
       public String initView(String id , String categoryId, Model model){
       	if( StringHelper.isNotBlank(id) ) {
       		
       		Product temp  = productService.get(id);
    		
       		model.addAttribute("product",iticJsonMapper.toJson(temp));
       		
       		ProductSkuExample example = new ProductSkuExample();
       		example.setOrderByClause("IS_MAIN desc");
       		com.itic.mall.system.productsku.persistence.model.ProductSkuExample.Criteria criteria = example.createCriteria();
       		criteria.andIsEffectiveEqualTo(DBConst.TRUE);
       		criteria.andProductIdEqualTo(id);
       		List<ProductSku> list = productSkuService.list(example);
       		model.addAttribute("productSkus",iticJsonMapper.toJson(list));
       		
       		model.addAttribute("name_china", MODEL_NAME_CHINA);
           	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
           	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
           	model.addAttribute("common_url", COMMON_URL);
           	model.addAttribute("categoryId", categoryId);
           	
       	}else {
               throw new ParameterErrorException();
           }
       	
       	return PAGE_COMMON_PATH+"View";
       }
       
       /*
        * 导入页面
        */
       @RequestMapping(value="/initImp")
       public String initImp(Model model){
       	model.addAttribute("name_china", MODEL_NAME_CHINA);
       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
       	model.addAttribute("parent_common_url", PARENT_COMMON_URL);
       	model.addAttribute("common_url", COMMON_URL);
       	return PAGE_COMMON_PATH+"Imp";
       }
       
       
       //////////////////////////////////////////////////////业务代码区////////////////////////////////////////////////////////////
       
       /**
        * 添加模板管理数据
        * @param android
        * @return
        */
       @RequestMapping(value="/add")
       @ResponseBody
       public Map<String,String> add(Product product,ProductSku productSku){
       	
       	    Map<String, String> resultMap = new HashMap<String, String>();
            String result = ResponseCode.SERVER_ERROR.getCode();
            
            //判断是添加新的商品还是  只添加一个SKU商品
            if(  StringHelper.isNotBlank(productSku.getProductId()) ){
            	result = productSkuService.add(productSku);
            }else{
            	 //将本类型商品下添加一个统计数量
                Category category = categoryService.get(product.getCategoryId());
                String count = "0";
                
                if( StringHelper.isNotBlank(category.getCount()) ){
                	count = category.getCount();
                }
                count = NumberStringHelper.addString(count, "1");
                category.setCount(count);
                
            	//添加新的商品
                product.setIndexLocation("0");
            	product.setTotalSales("0");
                product.setTotalFeedback("0");
                product.setTotalFeedbackGood("0");
                product.setPercentFeedback("100");
                product.setProductNum( RandomOrderNumHelper.getOrderNumber() );
                //算一下根据佣金 比例算一下卖一件可以回扣多少钱
                String marketProfit = "0";
                if( StringHelper.isNotBlank(product.getMarketProfitPercent()) ){
                	String replace = product.getMarketProfitPercent();
                	String orginPrice = product.getProductOrginPrice();
                	String productHongbao = product.getProductHongbao();
                	String price = NumberStringHelper.subtractString(orginPrice, productHongbao);
                	marketProfit = NumberStringHelper.mulString(replace, price);
                	marketProfit = NumberStringHelper.divString(marketProfit, "100", 2);
                }
                product.setMarketProfit(marketProfit);
                
                if( "1".equals(productSku.getIsMain()) ){
                	product.setProductOrginPrice(productSku.getProductOrginPrice());
                    product.setProductHongbao(product.getProductHongbao());
                    String productPrice = NumberStringHelper.subtractString(productSku.getProductOrginPrice(), product.getProductHongbao());
                    product.setProductPrice(productPrice);
                    //product.setProductPrice(productSku.getProductPrice());
                }
                
                
                String productId = IdGen.uuid();
                String productSkuId = IdGen.uuid();
                Date date = new Date();
                
                //产品的必填字段
                product.setId(productId);
                product.setIsEffective(DBConst.TRUE);
                product.setCreateTime(date);
                product.setUpdateTime(date);
                product.setCreateUser(null != UserSessionHelper.get(Constants.USER_ID) ? UserSessionHelper
                        .get(Constants.USER_ID) : "unknown");
                product.setOrgCode(null != UserSessionHelper.get(Constants.USER_ORG) ? UserSessionHelper
                        .get(Constants.USER_ORG) : "unknown");
                
               
                
                //产品SKU的必填字段
                productSku.setId(productSkuId);
                productSku.setIsEffective(DBConst.TRUE);
                productSku.setCreateTime(date);
                productSku.setUpdateTime(date);
                productSku.setCreateUser(null != UserSessionHelper.get(Constants.USER_ID) ? UserSessionHelper
                        .get(Constants.USER_ID) : "unknown");
                productSku.setOrgCode(null != UserSessionHelper.get(Constants.USER_ORG) ? UserSessionHelper
                        .get(Constants.USER_ORG) : "unknown");
                
                //关联的业务字段
                productSku.setProductId(productId);
                product.setMarketProfitPercent(product.getMarketProfitPercent());
                result = productService.addAndProductSku(category,product,productSku);
            }
            
            resultMap.put(IticConst.RESULT, result);
            resultMap.put(IticConst.DATA, productSku.getProductId());//将产品的SKU返回给前
            return resultMap;
            
            //别忘记了添加分类的商品统计 
       }
       
       /*
   	 * 查看页面
   	 */
   	@RequestMapping(value = "/order")
   	@ResponseBody
   	public  Map<String, String> order(String id, Integer reorder) {
   		 Map<String, String> resultMap = new HashMap<String, String>();
            String result = ResponseCode.SERVER_ERROR.getCode();
            
            if ( StringHelper.isNotBlank(id) ) {
           	 Product temp = productService.get(id);
           	 //temp.setReorder(reorder);
           	 //找到原来的排序，变成当前的
           	 ProductExample example = new ProductExample();
           	 example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andReorderEqualTo(reorder);
           	 List<Product> list = productService.list(example);
           	 if( list.size() > 0 ){
           		 for( Product tmp : list ){
           			 tmp.setReorder(temp.getReorder());
           			 productService.edit(tmp);
           		 }
           	 }
           	 temp.setReorder(reorder);
           	 result = productService.edit(temp);
            }
            
            resultMap.put(IticConst.RESULT, result);
            return resultMap;
   	}
   	
       
       
       /**
        * <p>Title:queryCondition </p>
        * <p>Description:封装查询条件 </p>
        * @param jzdw
        * @param jzdwExample
        * @return
        */
       private ProductExample queryCondition(HttpServletRequest request,Product temp) {
       	
       	   // 参数处理
           ProductExample example = new ProductExample();
           // 按创建时间倒序
           //example.setOrderByClause("create_time desc");
           // 按排列号顺序
           example.setOrderByClause("reorder asc");
           
           Criteria criteria = example.createCriteria().andIsEffectiveEqualTo( DBConst.TRUE );
           
           if( StringHelper.isNotBlank(temp.getCategoryId()) && !"0".equals(temp.getCategoryId())){
        	   //过虑类别条件
               criteria.andCategoryIdEqualTo(temp.getCategoryId());
           }
           
           // 查询条件
           if ( null != temp ) {
       	       //模糊查询
               if ( StringHelper.isNotBlank( temp.getName() ) ) {
                   criteria.andNameLike("%" + temp.getName() + "%");
               }
               
           }
           return example;
       }
       
       /**
        * 
        * @param goldType
        * @param request
        * @return
        */
       @RequestMapping(value = "/list")
       @ResponseBody
       public Map<String, Object> list(Product temp, HttpServletRequest request){
           // 声明返回对象
           Map<String, Object> resultMap = new HashMap<String, Object>();
           
           
           ProductExample example = this.queryCondition(request,temp);
           // 封装分页参数
           example = this.handlePageQuery(request, example);
           
           
           // 获取信息总条数
           int total = productService.count(example);
           
           // 获取数据
           List<Product> list = productService.list(example);
           
           // 数据返回处理
           resultMap.put(IticConst.TOTAL_RECORDS, total);
           resultMap.put(IticConst.DISPLAY_RECORDS, total);
           resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
           resultMap.put(IticConst.DATA, list);
           return resultMap;

       }
       
       /**
        * 编辑保存
        * @param Category
        * @return
        */
       @RequestMapping(value="/edit")
       @ResponseBody
       public Map<String,String> edit(Product temp){
       
       	    Map<String, String> resultMap = new HashMap<String, String>();
            String result = ResponseCode.SERVER_ERROR.getCode();
            //如果红包有变动
            if( StringHelper.isNotBlank(temp.getProductHongbao())){
            	ProductSkuExample example = new ProductSkuExample();
            	example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andIsMainEqualTo("1").andProductIdEqualTo(temp.getId());
            	List<ProductSku> list = productSkuService.list(example);
            	if( list.size()>0){
            		ProductSku productSku = list.get(0);
            		temp.setProductOrginPrice(productSku.getProductOrginPrice());
                    String productPrice = NumberStringHelper.subtractString(productSku.getProductOrginPrice(), temp.getProductHongbao());
                    temp.setProductPrice(productPrice);
                    //算一下根据佣金 比例算一下卖一件可以回扣多少钱
                    String marketProfit = "0";
                    if( StringHelper.isNotBlank(temp.getMarketProfitPercent()) ){
                    	String replace = temp.getMarketProfitPercent();
                    	String orginPrice = temp.getProductOrginPrice();
                    	String productHongbao = temp.getProductHongbao();
                    	String price = NumberStringHelper.subtractString(orginPrice, productHongbao);
                    	marketProfit = NumberStringHelper.mulString(replace, price);
                    	marketProfit = NumberStringHelper.divString(marketProfit, "100", 2);
                    }
                    temp.setMarketProfit(marketProfit);
                    
                    productService.edit(temp);
            	}
            }
            result = productService.edit(temp);
            resultMap.put(IticConst.RESULT, result);
            return resultMap;
       }
       
       /**
        * 编辑保存
        * @param Category
        * @return
        */
       @RequestMapping(value="/viewSku")
       @ResponseBody
       public ProductSku viewSku(String id){
            ProductSku sku = productSkuService.get(id);
            return sku;
       }
       
       
       /**
        * 编辑保存
        * @param Category
        * @return
        */
       @RequestMapping(value="/editSku")
       @ResponseBody
       public Map<String,String> editSku( ProductSku temp ){
       
       	    Map<String, String> resultMap = new HashMap<String, String>();
            String result = ResponseCode.SERVER_ERROR.getCode();
            //判断是否为主商品,如果有主商品,则更新商品的参价
            String isMain = temp.getIsMain();
            if( "1".equals(isMain) ){
            	ProductSku tmp = productSkuService.get(temp.getId());
            	Product product = productService.get(tmp.getProductId());
            	if( null != product ){
            		product.setProductOrginPrice(tmp.getProductOrginPrice());
                    String productPrice = NumberStringHelper.subtractString(tmp.getProductOrginPrice(), product.getProductHongbao());
                    product.setProductPrice(productPrice);
                    productService.edit(product);
            	}
            }
            
            result = productSkuService.edit(temp);
            
            resultMap.put(IticConst.RESULT, result);
            return resultMap;
       }
       
       
       

       /**
        * 删除
        * 
        */
       @RequestMapping(value = "/del")
       @ResponseBody
       public Map<String, String> del(String id) {
           Map<String, String> resultMap = new HashMap<String, String>();
           String result = ResponseCode.SERVER_ERROR.getCode();

           //删除一个
           //当前商品的数量减一
           Product product = productService.get(id);
           Category category = categoryService.get(product.getCategoryId());
           if( null != category ){
        	   String count = "0";
               if( StringHelper.isNotBlank(category.getCount()) ){
            	   count = category.getCount();
               }
               count = NumberStringHelper.subtractString(count, "1");
               category.setCount(count);
           }
           
           if( StringHelper.isNotBlank(id) ){
               Product temp = new Product();
               temp.setId(id);
               temp.setIsEffective(DBConst.FALSE);
               result = productService.deleteAndSku(category,temp);
           }
           
           resultMap.put(IticConst.RESULT, result);
           return resultMap;
       }
       
       /**
        * 删除
        * 
        */
       @RequestMapping(value = "hongbao")
       @ResponseBody
       public Map<String, Object> hongbao( String id, String hongbao, String startTime,String stopTime ) {
           Map<String, Object> resultMap = new HashMap<String, Object>();
           String result = ResponseCode.SERVER_ERROR.getCode();

           //删除一个
           //当前商品的数量减一
           Product product = productService.get(id);
          
           if(  DateHelper.isGreater(DateHelper.formatDateTime(new Date()), product.getStopTime()+" 23:59:59") ){
        	   product.setStartTime(startTime);
               product.setStopTime(stopTime);
               product.setProductHongbao(hongbao);
               result = productService.edit(product);
               resultMap.put("data", "success");
           }else{
        	   result = "sucess";
        	   resultMap.put("data", "fail");
        	   resultMap.put("msg", "红包未到期，暂时不能设置！");
           }
           resultMap.put(IticConst.RESULT, result);
           return resultMap;
       } 
       
       /**
        * 删除
        * 
        */
       @RequestMapping(value = "baokuan")
       @ResponseBody
       public Map<String, Object> baokuan( String id, Integer baokuanOrder ) {
           Map<String, Object> resultMap = new HashMap<String, Object>();
           String result = ResponseCode.SERVER_ERROR.getCode();

           if ( StringHelper.isNotBlank(id) ) {
          	 Product temp = productService.get(id);
          	 //temp.setReorder(reorder);
          	 //找到原来的排序，变成当前的
          	 ProductExample example = new ProductExample();
          	 example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andBaokuanOrderEqualTo(baokuanOrder);
          	 List<Product> list = productService.list(example);
          	 if( list.size() > 0 ){
          		 for( Product tmp : list ){
          			 tmp.setBaokuanOrder(temp.getBaokuanOrder());
          			 productService.edit(tmp);
          		 }
          	 }
          	 temp.setIsBaokuan("001");
          	 temp.setBaokuanOrder(baokuanOrder);
          	 result = productService.edit(temp);
           }
           
           resultMap.put(IticConst.RESULT, result);
           return resultMap;
       } 
       
       
       /**
        * 删除
        * 
        */
       @RequestMapping(value = "jingxuan")
       @ResponseBody
       public Map<String, Object> jingxuan( String id, Integer jingxuanOrder ) {
           Map<String, Object> resultMap = new HashMap<String, Object>();
           String result = ResponseCode.SERVER_ERROR.getCode();

           if ( StringHelper.isNotBlank(id) ) {
          	 Product temp = productService.get(id);
          	 //temp.setReorder(reorder);
          	 //找到原来的排序，变成当前的
          	 ProductExample example = new ProductExample();
          	 example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE).andJingpingOrderEqualTo(jingxuanOrder);
          	 List<Product> list = productService.list(example);
          	 if( list.size() > 0 ){
          		 for( Product tmp : list ){
          			 tmp.setJingpingOrder(temp.getJingpingOrder());
          			 productService.edit(tmp);
          		 }
          	 }
          	 temp.setIsJingping("001");
          	 temp.setJingpingOrder(jingxuanOrder);
          	 result = productService.edit(temp);
           }
           
           resultMap.put(IticConst.RESULT, result);
           return resultMap;
       } 
       
       /**
        * 获取所有的商品属性
        * 
        */
       @RequestMapping(value = "/delJingxuan")
       @ResponseBody
       public Map<String,String> delJingxuan( String id ){
    	   Map<String, String> resultMap = new HashMap<String, String>();
           String result = ResponseCode.SERVER_ERROR.getCode();
           
           if ( StringHelper.isNotBlank(id) ) {
          	 Product product = productService.get(id);
          	 product.setIsJingping("002");
          	 result = productService.edit(product);
           }
           
           resultMap.put(IticConst.RESULT, result);
           return resultMap;
       }
       
       /**
        * 获取所有的商品属性
        * 
        */
       @RequestMapping(value = "/delBaokuan")
       @ResponseBody
       public Map<String,String> delBaokuan( String id ){
    	   Map<String, String> resultMap = new HashMap<String, String>();
           String result = ResponseCode.SERVER_ERROR.getCode();
           
           if ( StringHelper.isNotBlank(id) ) {
          	 Product product = productService.get(id);
          	 product.setIsBaokuan("002");
          	 result = productService.edit(product);
           }
           
           resultMap.put(IticConst.RESULT, result);
           return resultMap;
       }
       
       /**
        * 获取所有的商品属性
        * 
        */
       @RequestMapping(value = "/getProductPropertys")
       @ResponseBody
       public List<ProductProperty> getProductPropertys() {
          ProductPropertyExample example = new ProductPropertyExample();
          example.setOrderByClause("reorder asc");
          com.itic.mall.system.productproperty.persistence.model.ProductPropertyExample.Criteria criteria = example.createCriteria();
          criteria.andIsEffectiveEqualTo(DBConst.TRUE);
          List<ProductProperty> list = productPropertyService.list(example);
          return list;
       }
       
       
       /**
        * 根据商品的一个属性获得所有的属性值
        * 
        */
       @RequestMapping(value = "/getProductPropertyValues")
       @ResponseBody
       public List<ProductPropertyValue> getProductPropertyValues(String propertyId) {
    	   ProductPropertyValueExample example = new ProductPropertyValueExample();
    	   example.setOrderByClause("reorder asc");
    	   com.itic.mall.system.productpropertyvalue.persistence.model.ProductPropertyValueExample.Criteria criteria = example.createCriteria();
    	   criteria.andIsEffectiveEqualTo(DBConst.TRUE).andPropertyIdEqualTo(propertyId);
    	   List<ProductPropertyValue> list = productPropertyValueService.list(example);
    	   return list;
           
       }
       
       /**
        * 根据ID获取所有的商品属性名称
        * 
        */
       @RequestMapping(value = "/getProductPropertyName")
       @ResponseBody
       public ProductProperty getProductPropertyName( String id ) {
          ProductProperty productProperty = productPropertyService.get(id);
          return productProperty;
       }
       
       /**
        * 根据ID获取所有的商品属性值名称
        * 
        */
       @RequestMapping(value = "/getProductPropertyValueName")
       @ResponseBody
       public ProductPropertyValue getProductPropertyValueName( String id ) {
          ProductPropertyValue productPropertyValue = productPropertyValueService.get(id);
          return productPropertyValue;
       }
       
       
       /**
        * 
        * @param goldType
        * @param request
        * @return
        */
       @RequestMapping(value = "/companys")
       @ResponseBody
       public List<Company> companys(HttpServletRequest request){
    	 	// 参数处理
           CompanyExample example = new CompanyExample();
           // 按创建时间倒序
           example.setOrderByClause("create_time desc");
           // 按排列号顺序
           example.setOrderByClause("reorder asc");
           
           example.createCriteria().andIsEffectiveEqualTo( DBConst.TRUE );
           // 获取数据
           List<Company> list = companyService.list(example);
           return list;

       }
       
}